// pages/category/category.js
import request from '../../utils/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {

        categoryList:[],
        goodsList:[],
        cid: 1,
        isShow: false,
        pageNum: 1,
        pageSize: 10,
        isShowMask: true,

        // 状态栏高度
        statusBarHeight: wx.getStorageSync('statusBarHeight') + 'px',
        statusBarPaddingTop: wx.getStorageSync('statusBarHeight') + 58 ,
        statusBarPaddingLine:750 - wx.getStorageSync('statusBarHeight') - 120 ,
        // 导航栏高度
        navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',
        // 胶囊按钮高度
        menuButtonHeight: wx.getStorageSync('menuButtonHeight') + 'px',
        // 导航栏和状态栏高度
        navigationBarAndStatusBarHeight:
            wx.getStorageSync('statusBarHeight') +
            wx.getStorageSync('navigationBarHeight') +
            'px'
    },
    handleGoodsContent(e){
        console.log(e.currentTarget.dataset.cid)
        this.setData({
            pageNum: 1
        })
        this.getGoodsList(e.currentTarget.dataset.cid)
        this.setData({
            cid:e.currentTarget.dataset.cid
        })
    },
    toGoodsDetail(e){
        wx.navigateTo({
          url: '/pages/goodsInfo/goodsInfo?gid='+e.currentTarget.dataset.gid,
        })
    },
    async addCartGoods(e){
        let gid = e.currentTarget.dataset.gid;
        let userInfo = wx.getStorageSync('youxuan-user-userInfo')
        console.log(userInfo.id,gid)
        let result = await request("/cart/add/goods",{goodsId:gid,num:1,userId:userInfo.id},"POST")
        console.log(result)
        if (result.code == 200) {
            wx.showToast({
              title: '添加成功',
              icon:"none"
            })
        }
    },
    goSearchGoods(){
        wx.navigateTo({
          url: '/pages/searchGoods/searchGoods',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {


        var categorysResult = await request('/category/list')
        this.setData({
            categoryList: categorysResult.data
        })

       
        this.getGoodsList(categorysResult.data[0].id)
       

        // setTimeout(() => {
        //     this.setData({
        //         isShowMask: false
        //     })
        // }, 200);


        const { statusBarHeight, platform } = wx.getSystemInfoSync()
        const { top, height } = wx.getMenuButtonBoundingClientRect()

        // 状态栏高度
        wx.setStorageSync('statusBarHeight', statusBarHeight)
        // 胶囊按钮高度 一般是32 如果获取不到就使用32
        wx.setStorageSync('menuButtonHeight', height ? height : 32)

        // 判断胶囊按钮信息是否成功获取
        if (top && top !== 0 && height && height !== 0) {
            const navigationBarHeight = (top - statusBarHeight) * 2 + height
            // 导航栏高度
            wx.setStorageSync('navigationBarHeight', navigationBarHeight)
        } else {
            wx.setStorageSync(
                'navigationBarHeight',
                platform === 'android' ? 48 : 40
            )
        }

        this.setData({
            isShowMask: false
        })
    },
    async getGoodsList(cid){
        var goodsListResult = await request('/goods/list?pageNum='+this.data.pageNum+'&pageSize='+this.data.pageSize+'&categoryId='+cid)
        this.setData({
            goodsList: goodsListResult.data.rows
        })
        console.log(goodsListResult.data.rows)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        setTimeout(() => {
            this.setData({
                isShowMask: false
            })
        }, 500);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.setData({
            isShowMask:true
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
       
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: async function () {
        this.setData({
            pageNum: this.data.pageNum + 1
        })
        var goodsListResult = await request("/goods/list?pageNum="+this.data.pageNum+"&pageSize="+this.data.pageSize+'&categoryId='+this.data.cid)
        console.log('触底了',goodsListResult.data.rows.length)
        if(goodsListResult.data.rows.length > 0){
            this.setData({
                goodsList: this.data.goodsList.concat(goodsListResult.data.rows)
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})